// pages/daili/daili
import { areaList } from "../../components/area-data/dist/index.cjs";
const app = getApp();
import request from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: "成为代理",
    areaList,
    showcompanytype: false,
    companytypeList: ["国营", "集体", "私营", "合资"],
    showsex: false,
    showarea: false,
    columns: ["个体工商户", "个人", "企业"],
    sexList: ["男", "女"],
    showqualification: false,
    showzizhi: false,
    id: null,
    memberid: null,
    name: "",
    sex: "",
    phone: null,
    job: "",
    area: "",
    onproxytype: "1",
    onaskforyear: null,
    onaskerposition: "",
    onproxylevel: "",
    qualification: "",
    oncompanyname: "",
    oncompanyaddress: "",
    onlegalperson: "",
    onregisteredTime: "",
    onregisteredcapital: "",
    oncompanybusiness: "",
    onexperience: "",
    onadvantage: "",
    onagreeyear: "",
    onexpiretime: "",
    oncompanytype: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.titlle) {
      this.setData({
        title: options.titlle,
      });
      this.getmessage();
    }
  },
  async getmessage() {
    const id = wx.getStorageSync("userId");
    const res = await request({
      url: app.globalData.baseURL + "/app-user-api/member/proxy-user/getProxyUser",
      method: "POST",
      data: {
        "memberId": id,
        "status": 1
      },
    });
    if(res.code === 0){
      this.setData({
        id: res.data[0].id,
        memberid: res.data[0].memberId,
        name: res.data[0].name,
        sex: res.data[0].sex == 1 ? "男" : "女",
        phone: res.data[0].phone,
        job: res.data[0].job,
        area: res.data[0].proxyAreaName,
        onproxytype: res.data[0].proxyType == 1 ? "区域独家代理" : "",
        onaskforyear: res.data[0].askForYear,
        onaskerposition: res.data[0].askerPosition,
        onproxylevel: res.data[0].proxyLevel,
        qualification:
          res.data[0].qualification == 1
            ? "个人"
            : res.data[0].qualification == 2
            ? "企业"
            : "个体工商户",
        oncompanyname: res.data[0].companyName,
        oncompanyaddress: res.data[0].companyAddress,
        onlegalperson: res.data[0].legalPerson,
        onregisteredTime: res.data[0].registeredTime,
        onregisteredcapital: res.data[0].registeredCapital,
        oncompanybusiness: res.data[0].companyBusiness,
        onexperience: res.data[0].experience,
        onadvantage: res.data[0].advantage,
        onagreeyear: res.data[0].agreeYear,
        onexpiretime: res.data[0].expireTime,
        oncompanytype: res.data[0].companyType,
      });
    }
    if (res.data[0].qualification !== 1) {
      this.setData({
        showzizhi: true,
      });
    }
  },
  async submit() {
    const id = wx.getStorageSync("userId");
    const formdata = {
      memberId: id,
      name: this.data.name,
      sex: this.data.sex == "男" ? 1 : 2,
      phone: this.data.phone,
      job: '无',
      proxyArea: this.data.area,
      proxyType: this.data.onproxytype,
      askForYear: 1,
      status: 2,
      proxyLevel: 1,
      qualification:
        this.data.qualification == "个人"
          ? 1
          : this.data.qualification == "企业"
          ? 2
          : 3,
      askerPosition: this.data.onaskerposition,
      companyName: this.data.oncompanyname,
      companyAddress: this.data.oncompanyaddress,
      legalPerson: this.data.onlegalperson,
      companyType:
        this.data.oncompanytype == "国营"
          ? 1
          : this.data.oncompanytype == "集体"
          ? 2
          : this.data.oncompanytype == "私营"
          ? 3
          : 4,
      registeredTime: this.data.onregisteredTime,
      registeredCapital: this.data.onregisteredcapital,
      companyBusiness: this.data.oncompanybusiness,
      experience: this.data.onexperience,
      advantage: this.data.onadvantage,
      agreeYear: this.data.onagreeyear,
      expireTime: this.data.onexpiretime,
    };
    const form = {
      memberId: id,
      name: this.data.name,
      sex: this.data.sex == "男" ? 1 : 2,
      phone: this.data.phone,
      job: '无',
      proxyArea: this.data.area,
      proxyType: this.data.onproxytype,
      askForYear: 1,
      status: 2,
      proxyLevel: 1,
      qualification:
        this.data.qualification == "个人"
          ? 1
          : this.data.qualification == "企业"
          ? 2
          : 3,
    };
    if (this.data.qualification !== "个人") {
      const res = await request({
        url: app.globalData.baseURL + "/app-user-api/member/proxy-user/create",
        method: "POST",
        data: formdata,
      });
      if (res.code == 0) {
        wx.showToast({
          title: "提交成功",
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    } else {
      const res = await request({
        url: app.globalData.baseURL + "/app-user-api/member/proxy-user/create",
        method: "POST",
        data: form,
      });
      if (res.code == 0) {
        wx.showToast({
          title: "提交成功",
        });
      } else {
        wx.showToast({
          title: res.msg,
        });
      }
    }
  },
  onID(e) {
    this.setData({
      id: e.detail.value,
    });
  },
  onName(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  onMemberid(e) {
    this.setData({
      memberid: e.detail.value,
    });
  },
  onSex() {
    this.setData({
      showsex: true,
    });
  },
  onCancelSex() {
    this.setData({
      showsex: false,
    });
  },
  onConfirmSex(e) {
    this.setData({
      sex: e.detail.value,
      showsex: false,
    });
  },
  onJob(e) {
    this.setData({
      job: e.detail.value,
    });
  },
  onPhone(e) {
    this.setData({
      phone: e.detail.value,
    });
  },
  onProxyType(e) {
    this.setData({
      onproxytype: e.detail.value,
    });
  },
  onAskforYear(e) {
    this.setData({
      onaskforyear: e.detail.value,
    });
  },
  onProxyLevel(e) {
    this.setData({
      onproxylevel: e.detail.value,
    });
  },
  onCompanyName(e) {
    this.setData({
      oncompanyname: e.detail.value,
    });
  },
  onCompanyAddress(e) {
    this.setData({
      oncompanyaddress: e.detail.value,
    });
  },
  onLegalPerson(e) {
    this.setData({
      onlegalperson: e.detail.value,
    });
  },
  onCompanyType() {
    this.setData({
      showcompanytype: true,
    });
  },
  onCancelcompanytype() {
    this.setData({
      showcompanytype: false,
    });
  },
  onConfirmcompanytype(e) {
    this.setData({
      oncompanytype: e.detail.value,
      showcompanytype: false,
    });
  },
  onRegiSteredTime(e) {
    this.setData({
      onregisteredTime: e.detail.value,
    });
  },
  onRegiSteredCapital(e) {
    this.setData({
      onregisteredcapital: e.detail.value,
    });
  },
  onCompanyBusiness(e) {
    this.setData({
      oncompanybusiness: e.detail.value,
    });
  },
  onExperience(e) {
    this.setData({
      onexperience: e.detail.value,
    });
  },
  onAdvantage(e) {
    this.setData({
      onadvantage: e.detail.value,
    });
  },
  onAgreeYear(e) {
    this.setData({
      onagreeyear: e.detail.value,
    });
  },
  onExpireTime(e) {
    this.setData({
      onexpiretime: e.detail.value,
    });
  },
  goback() {
    wx.navigateBack({
      delta: 1,
    });
  },
  areabind() {
    this.setData({
      showarea: true,
    });
  },
  confirm(e) {
    this.setData({
      area: e.detail.values[2].code,
      proxyarea: e.detail.values[2].name,
    });
    this.setData({
      showarea: false,
    });
  },
  cancel() {
    this.setData({
      showarea: false,
    });
  },
  onCancelqualification() {
    this.setData({
      showqualification: false,
    });
  },
  onConfirmqualification(e) {
    if (e.detail.value !== "个人") {
      this.setData({
        showqualification: false,
        qualification: e.detail.value,
        showzizhi: true,
      });
    } else {
      this.setData({
        showqualification: false,
        qualification: e.detail.value,
        showzizhi: false,
      });
    }
  },
  clear() {
    this.setData({
      id: null,
      memberid: null,
      name: "",
      sex: "",
      phone: null,
      job: "",
      area: "",
      onproxytype: "1",
      onaskforyear: null,
      onaskerposition: "",
      onproxylevel: "",
      qualification: "",
      proxyarea: "",
      oncompanyname: "",
      oncompanyaddress: "",
      onlegalperson: "",
      onregisteredTime: "",
      onregisteredcapital: "",
      oncompanybusiness: "",
      onexperience: "",
      onadvantage: "",
      onagreeyear: "",
      onexpiretime: "",
      oncompanytype: "",
      showzizhi: false,
    });
  },
  openqualification() {
    this.setData({
      showqualification: true,
    });
  },
  onAskerPosition(e) {
    this.setData({
      onaskerposition: e.detail.value,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
